import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/models/collectioninfomodel.dart';
import 'package:flutter_bmfmap_example/models/marketinfomodel.dart';
import 'package:flutter_bmfmap_example/pages/homepage/pages/Equipment_message/tool/tool.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/goodpage/evaluationList.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/shoppingCart/buyCar.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/submitOrders/SubmitOrders.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/submitOrders/submitOrderspage.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_svg/svg.dart';
// import 'package:flutter_bmfmap_example/pages/buyPage/buyPage.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

class GoodPage extends StatefulWidget {
  GoodPage({Key key, this.onValueChanged, this.id, this.url1, this.tagname})
      : super(key: key);

  final ValueChanged<int> onValueChanged;
  var id;
  String url1;
  String tagname;

  @override
  _GoodPageState createState() => _GoodPageState();
}

class _GoodPageState extends State<GoodPage> {
  var list = [];

  int num = 0;
  var info;
  var resulte;
  @override
  void initState() {
    // TODO: implement initState

    super.initState();
    print(widget.tagname);
    _getinformation();
  }

  _getinformation() async {
    list.add(widget.url1);
    EasyLoading.show(status: '加载中...');
    var result = await Http.get('/commodity/getById?id=' + widget.id.toString())
        .catchError((value) {
      Fluttertoast.showToast(msg: "服务器异常");
      EasyLoading.dismiss();
    });
    var resultevaluation = await Http.get(
            '/evaluate/getEvaluateByCommodityId?commodityId=' +
                widget.id.toString())
        .catchError((value) {
      Fluttertoast.showToast(msg: "服务器异常");
      EasyLoading.dismiss();
    });

    EasyLoading.dismiss();
    info = result;
    resulte = resultevaluation;
    print("**************************");
    print(result);
    print(resultevaluation);
    // list.clear();
    // list.add(result['imgurl1']);
    list.add(result['imgurl2']);
    list.add(result['imgurl3']);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Stack(
          alignment: Alignment.center,
          children: [
            Positioned(
              child: Container(
                // color: Colors.red,
                child: ListView(
                  children: [
                    buildStyle1(),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        decoration: new BoxDecoration(
                            color: Colors.white,
                            borderRadius: new BorderRadius.circular((16.0)),
                            boxShadow: [
                              BoxShadow(
                                  color: Colors.black12,
                                  offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                                  blurRadius: 5.0, //阴影模糊程度
                                  spreadRadius: 2 //阴影扩散程度
                                  )
                            ]),
                        child: Padding(
                          padding: const EdgeInsets.only(
                              left: 16.0, top: 20, right: 16, bottom: 20),
                          child: Column(children: [
                            Row(
                              children: [
                                Container(
                                  width: 180,
                                  child: Text(
                                    info == null ? "加载中" : info['name'],
                                    style: TextStyle(
                                        color: Colors.black87,
                                        fontSize: 24,
                                        fontWeight: FontWeight.w700),
                                  ),
                                ),
                                Spacer(),
                                // 右边的加减号
                                addOrjian(),
                              ],
                            ),
                            Row(
                              children: [
                                Padding(
                                  padding: const EdgeInsets.only(bottom: 8.0),
                                  child: Text(
                                    info == null ? '加载中' : '${info['money']}￥',
                                    style: TextStyle(
                                        color: Colors.red,
                                        fontSize: 20,
                                        fontWeight: FontWeight.w700),
                                  ),
                                ),
                              ],
                            ),
                            Container(
                              // width: 360,
                              child: Text(
                                info == null ? "加载中" : info['intro'],
                                style: TextStyle(
                                  color: Colors.black54,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                          ]),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        decoration: new BoxDecoration(
                            color: Colors.white,
                            borderRadius: new BorderRadius.circular((16.0)),
                            boxShadow: [
                              BoxShadow(
                                  color: Colors.black12,
                                  offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                                  blurRadius: 5.0, //阴影模糊程度
                                  spreadRadius: 2 //阴影扩散程度
                                  )
                            ]),
                        child: Padding(
                          padding: const EdgeInsets.only(
                              left: 16.0, top: 20, right: 16, bottom: 20),
                          child: Column(
                            children: [
                              Row(children: [
                                Text(
                                  '发货',
                                  style: TextStyle(color: Colors.black38),
                                ),
                                SizedBox(
                                  width: 16,
                                ),
                                Text('云南昆明'),
                                SizedBox(
                                  width: 16,
                                ),
                                Text(
                                  '|',
                                  style: TextStyle(color: Colors.black38),
                                ),
                                SizedBox(
                                  width: 16,
                                ),
                                Text('快递:0.00元'),
                              ]),
                              SizedBox(
                                height: 12,
                              ),
                              Row(children: [
                                Text(
                                  '保障',
                                  style: TextStyle(color: Colors.black38),
                                ),
                                SizedBox(
                                  width: 16,
                                ),
                                Text('七天无理由'),
                              ]),
                            ],
                          ),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        decoration: new BoxDecoration(
                            color: Colors.white,
                            borderRadius: new BorderRadius.circular((16.0)),
                            boxShadow: [
                              BoxShadow(
                                  color: Colors.black12,
                                  offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                                  blurRadius: 5.0, //阴影模糊程度
                                  spreadRadius: 2 //阴影扩散程度
                                  )
                            ]),
                        child: Padding(
                          padding: const EdgeInsets.only(
                              left: 16.0, top: 20, right: 16, bottom: 20),
                          child: Column(
                            children: [
                              Row(children: [
                                Text(
                                  '宝贝评价',
                                  // style: TextStyle(color: Colors.black38),
                                ),
                                Spacer(),
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context)
                                        .push(MaterialPageRoute(
                                            builder: (context) => evaluationAll(
                                                  id: widget.id,
                                                )));
                                  },
                                  child: Text(
                                    '查看全部',
                                    style:
                                        TextStyle(color: Colors.orangeAccent),
                                  ),
                                ),
                                Center(
                                    child: Icon(Icons.arrow_forward_ios,
                                        size: 12, color: Colors.orangeAccent)),
                              ]),
                              Center(
                                  // height: 12,
                                  child: resulte == null
                                      ? Container()
                                      : Column(
                                          children: [
                                            if (resulte.length >= 1)
                                              evaluationList(resulte[0]),
                                            if (resulte.length >= 2)
                                              evaluationList(resulte[1]),
                                          ],
                                        )),
                              Row(children: [
                                Text(
                                  '保障',
                                  style: TextStyle(color: Colors.black38),
                                ),
                                SizedBox(
                                  width: 16,
                                ),
                                Text('七天无理由'),
                              ]),
                            ],
                          ),
                        ),
                      ),
                    ),
                    SizedBox(height: 120),
                  ],
                ),
              ),
            ),
            Positioned(
              top: 10,
              left: 10,
              child: InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  height: 40,
                  width: 40,
                  child: Image.asset('assets/images/icons/png/左箭头 向左 线性.png'),
                  // color: Colors.red,
                  decoration: new BoxDecoration(
                      color: Color.fromRGBO(242, 242, 249, 1),
                      borderRadius: new BorderRadius.circular((12.0)),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.black12,
                            offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                            blurRadius: 5.0, //阴影模糊程度
                            spreadRadius: 2 //阴影扩散程度
                            )
                      ]),
                ),
              ),
            ),
            Positioned(
              top: 10,
              right: 10,
              child: InkWell(
                onTap: () {
                  setState(() {});
                },
                child: Container(
                  height: 40,
                  width: 40,
                  child: Image.asset('assets/images/icons/png/警告.png'),
                  decoration: new BoxDecoration(
                      color: Color.fromRGBO(242, 242, 249, 1),
                      borderRadius: new BorderRadius.circular((12.0)),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.black12,
                            offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                            blurRadius: 5.0, //阴影模糊程度
                            spreadRadius: 2 //阴影扩散程度
                            )
                      ]),
                ),
              ),
            ),
            Positioned(
              // 最下面的框
              bottom: 32,
              // left: 20,
              child: Container(
                // 总体的框
                height: 56,
                width: MediaQuery.of(context).size.width - 32,
                child: Padding(
                  // 总体居中;
                  padding: const EdgeInsets.only(left: 16.0, right: 16),
                  child: Row(
                    children: [
                      InkWell(
                        onTap: () {
                          Navigator.of(context).push(MaterialPageRoute(
                              builder: (context) => BuycarPage()));
                        },
                        child: Container(
                          width: 40,
                          height: 40,
                          child: Stack(alignment: Alignment.center, children: [
                            Container(
                              width: 32,
                              height: 32,
                              decoration: new BoxDecoration(
                                color: Color.fromRGBO(69, 177, 255, 1),
                                borderRadius: new BorderRadius.circular((12.0)),
                              ),
                              child: Center(
                                child: SvgPicture.asset(
                                  'assets/images/icons/svg/购物车.svg',
                                  color: Colors.white,
                                  width: 24,
                                ),
                              ),
                            ),
                            Positioned(
                              left: 24,
                              top: 0,
                              child: Container(
                                width: 16,
                                height: 16,
                                decoration: new BoxDecoration(
                                  color: Color.fromRGBO(255, 20, 20, 1),
                                  //设置四周圆角 角度
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(8.0)),
                                ),
                                child: Center(
                                  child: Text(
                                    context
                                        .watch<Shoppingcatmodel>()
                                        .getshoppingcattotalnum()
                                        .toString(),
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 10,
                                        fontWeight: FontWeight.w700),
                                  ),
                                ),
                              ),
                            ),
                          ]),
                        ),
                      ),

                      SizedBox(
                        width: 8,
                      ),
                      InkWell(
                        onTap: () {
                          context
                              .read<Collectioninfomodel>()
                              .changecollectionstzte(widget.id);
                        },
                        child: Container(
                          width: 32,
                          height: 32,
                          decoration: new BoxDecoration(
                            color: Color.fromRGBO(69, 177, 255, 1),
                            borderRadius: new BorderRadius.circular((12.0)),
                          ),
                          child: Center(
                            child: SvgPicture.asset(
                              'assets/images/icons/svg/收藏.svg',
                              color: context
                                      .watch<Collectioninfomodel>()
                                      .iscollection(widget.id)
                                  ? Colors.red
                                  : Colors.white,
                              width: 20,
                            ),
                          ),
                        ),
                      ),

                      Spacer(),
                      InkWell(
                        onTap: () {
                          context
                              .read<Shoppingcatmodel>()
                              .addshop(widget.id, num);
                        },
                        child: Container(
                          // width: 105,
                          padding: EdgeInsets.only(left: 16.0, right: 16),
                          height: 32,
                          decoration: new BoxDecoration(
                            color: Color.fromRGBO(69, 177, 255, 1),
                            borderRadius: new BorderRadius.circular((20.0)),
                          ),
                          margin: const EdgeInsets.only(left: 8.0, right: 8),
                          child: Center(
                            child: Text(
                              '加入到购物车',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 14,
                                  fontWeight: FontWeight.w600),
                            ),
                          ),
                        ),
                      ),

                      // Spacer(),
                      // Container(
                      //   height: 30,
                      //   width: 1,
                      //   color: Colors.white,
                      // ),
                      InkWell(
                        onTap: () {
                          // _showbuypage();
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => SubmitOrdersPage(
                                content: '${widget.id}x$num',
                                money: info['money'] * num,
                              ),
                            ),
                          );
                        },
                        child: Container(
                          padding: EdgeInsets.only(left: 16.0, right: 16),
                          height: 32,
                          decoration: new BoxDecoration(
                            color: Color.fromRGBO(69, 177, 255, 1),
                            borderRadius: new BorderRadius.circular((20.0)),
                          ),
                          margin: const EdgeInsets.only(left: .0, right: 8),
                          child: Center(
                            child: Text(
                              '立即购买',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 14,
                                  fontWeight: FontWeight.w600),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                // color: Colors.red,
                decoration: new BoxDecoration(
                    color: Colors.blue,
                    borderRadius: new BorderRadius.circular((12.0)),
                    boxShadow: [
                      BoxShadow(
                          color: Colors.blue,
                          offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                          blurRadius: 5.0, //阴影模糊程度
                          spreadRadius: 0 //阴影扩散程度
                          )
                    ]),
              ),
            ),
          ],
        ),
      ),
    );
  }

  evaluationList(var listOnly) {
    // print(listOnly);
    // print('=================');
    return Container(
      child: Padding(
        padding: const EdgeInsets.all(2.0),
        child: Column(
          children: [
            Container(
                margin: const EdgeInsets.all(2.0),
                // color: Color.fromRGBO(256, 256, 256, 0.1),
                width: 330,
                child: Column(children: [
                  Row(children: [
                    Container(
                      margin: const EdgeInsets.only(
                          left: 5, right: 10, top: 10, bottom: 10),
                      child: ClipOval(
                        child: Image.network(
                          listOnly['portrait'],
                          width: 38,
                          height: 38,
                        ),
                      ),
                    ),
                    Column(children: [
                      Container(width: 200, child: Text(listOnly['username'])),
                      Container(
                          width: 200,
                          child: Text(
                            Tool.getTime(listOnly['created']),
                            style: TextStyle(
                              color: Colors.black54,
                              fontSize: 14,
                            ),
                          ))
                    ]),
                  ]),
                  Container(
                    // margin: const EdgeInsets.all(4.0),
                    width: 320,
                    child: Text(listOnly['info']),
                  ),
                  Container(
                    width: 500,
                    // constraints: BoxConstraints(maxWidth: 500),
                    margin: const EdgeInsets.all(4.0),
                    child: Row(
                      children: [
                        if (listOnly['imgurl1'] != null)
                          Container(
                            margin: const EdgeInsets.all(2.0),
                            child: Image.network(listOnly['imgurl1'],
                                fit: BoxFit.contain, width: 75),
                          ),
                        if (listOnly['imgurl2'] != null)
                          Container(
                            margin: const EdgeInsets.all(2.0),
                            child: Image.network(listOnly['imgurl2'],
                                fit: BoxFit.contain, width: 75),
                          ),
                        if (listOnly['imgurl3'] != '')
                          Container(
                            margin: const EdgeInsets.all(2.0),
                            child: Image.network(listOnly['imgurl3'],
                                fit: BoxFit.contain, width: 75),
                          ),
                        if (listOnly['imgurl4'] != '')
                          Container(
                            margin: const EdgeInsets.all(2.0),
                            child: Image.network(listOnly['imgurl4'],
                                fit: BoxFit.contain, width: 75),
                          ),
                      ],
                    ),
                  ),
                ])),
            Divider(
              height: 10.0,
              indent: 0.0,
              color: Colors.black38,
            ),
          ],
        ),
      ),
    );
  }

  // 右边加减按钮
  addOrjian() {
    return Container(
      // width: 110,
      // height: 40,
      decoration: new BoxDecoration(
        color: Color.fromRGBO(219, 219, 219, 0.3),
        borderRadius: new BorderRadius.circular((15.0)),
      ),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            GestureDetector(
              onTap: _minusNum,
              child: Center(
                child: Container(
                  // color: Colors.red,
                  alignment: Alignment.center,
                  child: Center(
                    child: Icon(
                      Icons.remove,
                    ),
                  ), // 设计图
                  decoration: new BoxDecoration(
                      color: Color.fromRGBO(246, 247, 252, 1),
                      borderRadius: new BorderRadius.circular((5.0)),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.black12,
                            offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                            blurRadius: 5.0, //阴影模糊程度
                            spreadRadius: 0.1 //阴影扩散程度
                            )
                      ]),
                ),
              ),
            ),
            // Container(
            //   width: 0.5,
            //   color: Colors.grey,
            // ),
            Container(
              width: 30.0,
              alignment: Alignment.center,
              child: Text(
                num.toString(),
                maxLines: 1,
                style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.black,
                    fontWeight: FontWeight.w700),
              ),
            ),
            // Container(
            //   width: 0.5,
            //   color: Colors.grey,
            // ),
            GestureDetector(
              onTap: _addNum,
              child: Container(
                alignment: Alignment.center,
                child: Icon(Icons.add),
                decoration: new BoxDecoration(
                    color: Color.fromRGBO(242, 242, 249, 1),
                    borderRadius: new BorderRadius.circular((5.0)),
                    boxShadow: [
                      BoxShadow(
                          color: Colors.black12,
                          offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                          blurRadius: 5.0, //阴影模糊程度
                          spreadRadius: 0.1 //阴影扩散程度
                          )
                    ]),
              ),
            ),
          ],
        ),
      ),
    );
  }

  //自定圆点分页指示器
  buildSwiperPagination() {
    // 分页指示器
    return SwiperPagination(
      //指示器显示的位置
      alignment: Alignment.bottomCenter, // 位置 Alignment.bottomCenter 底部中间
      // 距离调整
      margin: const EdgeInsets.fromLTRB(0, 0, 0, 5),
      // 指示器构建
      builder: DotSwiperPaginationBuilder(
          // 点之间的间隔
          space: 2,
          // 没选中时的大小
          size: 6,
          // 选中时的大小
          activeSize: 12,
          // 没选中时的颜色
          color: Colors.black54,
          //选中时的颜色
          activeColor: Colors.black87),
    );
  }

//定义轮播图组件
  Widget buildStyle1() {
    return Hero(
      tag: widget.tagname,
      child: Container(
        height: 400.0,
        child: new Swiper(
          // 横向
          scrollDirection: Axis.horizontal,
          // 布局构建
          itemBuilder: (BuildContext context, int index) {
            return Padding(
              padding: const EdgeInsets.all(32.0),
              child: Image.network(
                list[index],
                fit: BoxFit.fill,
              ),
            );
          },
          //条目个数
          itemCount: list.length,
          // 自动翻页
          autoplay: true,
          // 分页指示
          pagination: buildSwiperPagination(),
          //点击事件
          onTap: (index) {
            print(" 点击 " + index.toString());
          },
          // 视窗比例
          viewportFraction: 1,
          // 布局方式
          //layout: SwiperLayout.STACK,
          // 用户进行操作时停止自动翻页
          autoplayDisableOnInteraction: true,
          // 无线轮播
          loop: true,
          scale: 1,
        ),
      ),
    );
  }

  void _minusNum() {
    if (num == 0) {
      return;
    }

    setState(() {
      num -= 1;
      // print(widget.num);
      if (widget.onValueChanged != null) {
        widget.onValueChanged(num);
      }
    });
  }

  void _addNum() {
    setState(() {
      num += 1;
      // print(widget.num);

      if (widget.onValueChanged != null) {
        widget.onValueChanged(num);
      }
    });
  }

  _showbuypage() {
    showModalBottomSheet(
        context: context,
        shape: RoundedRectangleBorder(
          // borderRadius: BorderRadius.circular(25),
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(25), topRight: Radius.circular(25)),
        ),
        builder: (BuildContext context2) {
          return StatefulBuilder(builder: (context2, setBottomSheetState) {
            return Container(
              padding: EdgeInsets.all(16),
              height: 600,
              child: SubmitOrders(),
            );
          });
        });
  }
}
